﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using DDOS;

namespace DDOS
{
    public class TransitionScreenLayer : ScreenLayer
    {
        private const int OFFSET = 50;
        private TransitionState transitionState;
        private Rectangle drawRectangle;
        private Rectangle sourceRectangle;
        private int expansionSpeed;
        private Color fadeColor;
        private Rectangle fadeRectangle;
        

        public TransitionScreenLayer(int expansionSpeed) :
            base(true)
        {
            transitionState = TransitionState.Growing;
            this.expansionSpeed = expansionSpeed;
            drawRectangle = new Rectangle(ScreenManager.Instance.ActualScreenBounds.Left, ScreenManager.Instance.ActualScreenBounds.Top, expansionSpeed, ScreenManager.Instance.ActualScreenBounds.Height);
            fadeRectangle = ScreenManager.Instance.ActualScreenBounds;
            sourceRectangle = new Rectangle(0, 0, ImageManager2D.Instance.WhiteBox.Width, ImageManager2D.Instance.WhiteBox.Height);
            fadeColor = Color.Black;
            fadeColor.A = byte.MinValue;
        }

        public override void Update(GameTime gt)
        {
            if (ScreenManager.Instance.TransitionType == Transition.Expand)
            {
                if (transitionState == TransitionState.Growing)
                {
                    drawRectangle.Width += expansionSpeed;
                    if (drawRectangle.Width > ScreenManager.Instance.ActualScreenBounds.Width + 2 * OFFSET)
                    {
                        transitionState = TransitionState.Shrinking;
                        ScreenManager.Instance.TransitionHalfwayPoint();
                    }
                }
                else
                {
                    drawRectangle.Width -= expansionSpeed;
                    drawRectangle.X += expansionSpeed;
                    if (drawRectangle.Width < OFFSET)
                    {
                        ScreenManager.Instance.TransitionEnded();
                    }
                }
            }
            if (ScreenManager.Instance.TransitionType == Transition.Fade)
            {
                if (transitionState == TransitionState.Growing)
                {
                    fadeColor.A += (byte)3;
                    if (fadeColor.A == byte.MaxValue)
                    {
                        transitionState = TransitionState.Shrinking;
                        ScreenManager.Instance.TransitionHalfwayPoint();
                    }
                    
                }
                else
                {
                    fadeColor.A -= (byte)3;
                    if (fadeColor.A == byte.MinValue)
                    {
                        ScreenManager.Instance.TransitionEnded();
                    }
                }
            }
        }
        

        public override void Draw()
        {
            if(ScreenManager.Instance.TransitionType == Transition.Expand)
                ScreenManager.Instance.CurrentSpriteBatch.Draw(ImageManager2D.Instance.WhiteBox, drawRectangle,sourceRectangle, Color.White,0f,Vector2.Zero,SpriteEffects.None,1f);
            if (ScreenManager.Instance.TransitionType == Transition.Fade)
                ScreenManager.Instance.CurrentSpriteBatch.Draw(ImageManager2D.Instance.WhiteBox, fadeRectangle, sourceRectangle, fadeColor, 0f, Vector2.Zero, SpriteEffects.None, 1f);
        }

        private enum TransitionState
        {
            Growing, Shrinking
        }
    }
}
